<?php
session_start();

include_once "../db_connect.php"; // Connect to the database
include_once ("agoTimeFormat.php");
//include_once ("filter.php");
$myAgoObject = new convertToAgo; // Establish the object
//$myfilterdata = new filterdata;
// Get the "id" URL variable and query the database for the original post of this thread
$thread_id = preg_replace('#[^0-9]#i', '', $_GET['id']); 
$sql = mysql_query("SELECT * FROM forum_posts WHERE id='$thread_id' AND type='a' LIMIT 1");
$numRows = mysql_num_rows($sql);
if ($numRows < 1) {
	echo "ERROR: That thread does not exist. Stop playing with the URLs.";
	exit();
}
    while($row = mysql_fetch_array($sql))
	{
		
		$post_author = $row["post_author"];
		$post_author_id = $row["post_author_id"];
		$date_time = $row["date_time"];
		$date_time = strftime("%b %d, %Y", strtotime($date_time));
		$section_title = $row["section_title"];
		$section_id = $row["section_id"];
		$thread_title = $row["thread_title"];
		$post_body = $row["post_body"];
		$tbl_id = $row["id"];
		$type = $row["type"];
		
		$post_button = $post_body .
		'
				
			
		';
	}
?>

<?php 
// Now query any responses out of the database and place in a dynamic list
$id = $_SESSION['id'];
//$thread_title = $_SESSION['$thread_title'];
$all_responses = "";
$sql = mysql_query("SELECT * FROM forum_posts WHERE otid='$thread_id' AND type='b'");
$numRows = mysql_num_rows($sql);
if ($numRows < 1) {
	$all_responses = '<div id="none_yet_div">Nobody has responded to this yet, you can be the first.</div>';
} else {
    while($row = mysql_fetch_array($sql)){
	$tbl_id = $row['id'];
	$reply_author = $row["post_author"];
	$reply_author_id = $row["post_author_id"];
	$date_n_time = $row["date_time"];
	$type = $row["type"];
	$convertedTime = $myAgoObject -> convert_datetime($date_n_time);
    $whenReply = $myAgoObject -> makeAgo($convertedTime);
	//$threadtitle = %rows['thread_title'];
	$reply_body = $row["post_body"];
	//$va=($myfilterdata -> filterbadwords($reply_body));
	$all_responses .= 
	'<div class="response_top_div">
		Re: ' . $thread_title . ' &nbsp; &nbsp; &bull; &nbsp; &nbsp; ' . $date_n_time. ' 
		<a href="#' . $reply_author_id . '">' . $reply_author . '</a> said:
	</div>
	<div class="response_div">' . $reply_body. 
	'

	</div>';
   }
}
?>

<?php 
// Be sure the user session vars are all set in order to show them the "replyButton"
$replyButton = 'You must <a href="../login.php">Log In</a> to respond';
if (isset($_SESSION['id']) && isset($_SESSION['username']) && isset($_SESSION['password'])) {
	$replyButton = '<input name="myBtn1" type="submit" value="Post a Response" style="font-size:16px; padding:12px;" " onmousedown="javascript:toggleForm(\'reponse_form\');" />';
}
// Check the database to be sure that their ID, password, and email session variables all match in the database
$u_id = mysql_real_escape_string($_SESSION['id']);
$u_name = mysql_real_escape_string($_SESSION['username']);
$u_pass = mysql_real_escape_string($_SESSION['password']);
$name = mysql_real_escape_string($_SESSION['fname']);
$sql = mysql_query("SELECT * FROM tbl_account WHERE id='$u_id' AND username='$u_name' AND password='$u_pass'");
$numRows = mysql_num_rows($sql);
if ($numRows < 1) {
	   $replyButton = 'You Must <a href="../.index.php">Log In</a> to Respond';
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style/style.css" rel="stylesheet" type="text/css" />
<script src="../js/jquery-1.8.3.js" type="text/javascript"></script>
<title><?php echo $thread_title; ?></title>
<script language="javascript" type="text/javascript">
function toggleForm(x) {
		if ($('#'+x).is(":hidden")) {
			$('#'+x).slideDown(200);
		} else {
			$('#'+x).slideUp(200);
		}
}

$('#responseForm').submit(function(){$('input[type=submit]', this).attr('disabled', 'disabled');});
function parseResponse ( ) {
	  var thread_id = $("#thread_id");
	  var forum_thread_title = $("#forum_thread_title");
	  var post_body = $("#post_body");
	  var fs_id = $("#forum_section_id");
	  var fs_title = $("#forum_section_title");
	  var u_id = $("#member_id");
	  var u_pass = $("#member_password");
	  var url = "parse_post.php";
      
	  if (post_body.val() == "") {
           $("#formError").html('<font size="+2">Please type your response.</font>').show().fadeOut(3000);
      } else if (post_body.val().length < 2 ) { 
	         $("#formError").html('<font size="+2">Your post must be at least 2 characters long').show().fadeOut(3000);
      } else {
		$("#myBtn1").hide();
		$("#formProcessGif").show();
        $.post(url,{ post_type: "b", tid: thread_id.val(), post_body: post_body.val(), fsID: fs_id.val(), fsTitle: fs_title.val(), thread_title: forum_thread_title.val() ,uid: u_id.val(), upass: u_pass.val() } , 
		
		function(data) {
			   $("#none_yet_div").hide();
			   var myDiv = document.getElementById('responses');
			   var magicdiv1 = document.createElement('div');
			   magicdiv1.setAttribute("class", "response_top_div");
			   magicdiv1.htmlContent = 'Re: <?php echo $thread_title?>';
			   magicdiv1.innerHTML = 'Re: <?php echo $thread_title ?>';

			   myDiv.appendChild(magicdiv1);
			   var magicdiv = document.createElement('div');
			   magicdiv.setAttribute("class", "response_div");
			   magicdiv.htmlContent = data;
			   magicdiv.innerHTML = data;
			   myDiv.appendChild(magicdiv);
			   $('#reponse_form').slideUp("fast");
			   document.responseForm.post_body.value='';
			   $("#formProcessGif").hide();
			   $("#myBtn1").show();
         }); 
	  }
}
//auto refresh



</script>

<style type="text/css">
.topic_div
{
	background-color: #D9F9FF;
	font-size:14px;
	padding:16px;
	border: #01B3D8 1px solid;
	margin-bottom:6px;
	font-weight: 500;
	color:#069;
}
.response_top_div
{
	background-color: #E4E4E4;
	color: #666;
	font-size:16px;
	padding:4px;
	border: #CCC 1px solid;
	border-bottom:none;
	color: #999;
}
.response_div
{
	background-color: #FFF;
	font-size:16px;
	padding:12px;
	border:#CCC 1px solid;
	margin-bottom:6px;
	width:900px;
	overflow:hidden;
}
#none_yet_div 
{
	background-color: #E4E4E4;
	font-size:14px;
	padding:16px;
	border: #CCC 1px solid;
	margin-bottom:6px;
	color: #999;
}

</style>
</head>
<body>
<table  width="900" border="0" align="center" cellpadding="12" cellspacing="0">
  <tr>
    <td width="731" valign="top" style="line-height:1.5em;">
    <div id="breadcrumbs">
	<a href="index.php" style="font-size:14px; font-family:verdana, Geneva, sans-serif;">Forum Home</a> &larr;
	<a href="section.php?id=<?php echo $section_id; ?>" style="font-size:14px; font-family:verdana, Geneva, sans-serif; "><?php echo $section_title; ?></a></div>
     
    <span class="topicTitles"><h1><?php echo $thread_title; ?></h1></span>
    <p style="font-size:14px; font-family:verdana, Geneva, sans-serif; text-align:justify;">Topic Started By: <a href="#"<?php echo $post_author_id; ?>"><?php echo $post_author; ?></a>
   &bull; Date-Created: <span class="topicCreationDate"><?php echo $date_time; ?></span></p>
    
    <div class="topic_div"><?php echo $post_button; ?></div>
	<div id="responses"><?php echo $all_responses; ?></div>

<!-- START DIV that contains the form -->
<div id="reponse_form" style="display:none; background-color:#BAE1FE; border:#06C 1px solid; padding:16px;">
	<form action="javascript:parseResponse();" name="responseForm" id="responseForm" method="post">
		Type your response below <?php echo $u_name; ?>:<br />
		<textarea name="post_body" id="post_body" cols="64" rows="12" style="width:98%;"></textarea>
		<div id="formError" style="display:none; padding:16px; color:#F00;"></div>
		<br /><br /><input name="myBtn1" id="myBtn1" type="submit" value="Submit Your Response" style="padding:6px;" /> <span id="formProcessGif" style="display:none;"><img src="../../Images/loading.gif" width="28" height="10" alt="Loading" vspace="2" hspace="48" /></span>
		or <a href="#" onclick="return false" onmousedown="javascript:toggleForm('reponse_form');">Cancel</a>
	
		<input name="thread_id" id="thread_id" type="hidden" value="<?php echo $thread_id; ?>" />
		<input name="forum_section_id" id="forum_section_id" type="hidden" value="<?php echo $section_id; ?>" />
		<input name="forum_section_title" id="forum_section_title" type="hidden" value="<?php echo $section_title; ?>" />
		<input name="forum_thread_title" id="forum_thread_title" type="hidden" value="<?php echo $thread_title; ?>" />
		<input name="member_id" id="member_id" type="hidden" value="<?php echo $_SESSION['id']; ?>" />
		<input name="member_password" id="member_password" type="hidden" value="<?php echo $_SESSION['password']; ?>" />
	</form>
</div>


<!-- END DIV that contains the form -->
<?php echo $replyButton; ?>
<br />
<br />
</td>
    </tr>
</table>
</body>
</html>